import { Injectable } from '@angular/core';
import { DfHTTP } from '../../lib/network/network.service';
import { NetworkConfig, LoginResponse } from '../../app/app.service';
import { Storage } from '@ionic/storage';

@Injectable()
export class LoginService {
    constructor(private dfHTTP: DfHTTP) { }
    login(params): Promise<any> {

        var paramStr = JSON.stringify(params);

        return this.dfHTTP.post(NetworkConfig.paths.login, paramStr)
            .then((res: LoginResponse) => {
                if (res.code === 'SUCCESS') {
                    NetworkConfig.headers.DF_KEY = res.data.token;
                }
                return res;
            });

    }

}

@Injectable()
export class UserService {
    constructor(private storage: Storage) { }
    getUser(): Promise<any> {
        return this.storage.get('lastUser');
    }
    setUser(user: any): void {
        this.storage.set('lastUser', user);
    }
    clearUser(): void {
        this.storage.remove('lastUser');
    }
}